package com.cskaoyan.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class CustomAspect {
    //JoinPoint可以直接作为before通知和around通知的形参
    public void before(JoinPoint joinPoint) {
        Object proxy = joinPoint.getThis();
        Object target = joinPoint.getTarget();
        System.out.println("代理对象：" + proxy);//注意这里打印的是委托类的内存地址
        System.out.println("目标对象：" + target);

        Signature signature = joinPoint.getSignature();
        String declaringTypeName = signature.getDeclaringTypeName();
        System.out.println(declaringTypeName);
        String name = signature.getName();
        System.out.println("方法名：" + name);

        Object[] args = joinPoint.getArgs();
        System.out.println(Arrays.asList(args));

        System.out.println("雪茄好");
    }

    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        if ("method3".equals(joinPoint.getSignature().getName())) {
            //找到特定的方法
            //对应的args → String
            args[0] = args[0] + " niupi";
        }

        System.out.println("around的前半部分");
        //Object proceed = joinPoint.proceed();//执行的时候，仍然是加工前的参数args
        //如果你委托类方法要执行的参数产生变化，使用这个有参的proceed方法
        Object proceed = joinPoint.proceed(args);
        System.out.println("around的后半部分");

        return proceed + "daqi";//会影响代理对象的执行结果
    }

}
